package handler

import (
	"context"
	xhttp "github.com/zeromicro/x/http"
	"net/http"
)

type Response struct {
}

func NewResponse() *Response {
	return &Response{}
}

// Result 通用返回结果
func (r *Response) Result(ctx context.Context, w http.ResponseWriter, data any, err error) {
	if err != nil {
		// code-data 响应格式
		xhttp.JsonBaseResponseCtx(ctx, w, err)
		return
	}
	// code-data 响应格式
	xhttp.JsonBaseResponseCtx(ctx, w, data)
	return
}

// Error 失败返回结果
func (r *Response) Error(ctx context.Context, w http.ResponseWriter, err error) {
	r.Result(ctx, w, nil, err)
}

// Success 成功返回结构
func (r *Response) Success(ctx context.Context, w http.ResponseWriter, data any) {
	r.Result(ctx, w, data, nil)
}
